package com.co.vote.web.controller.label;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.co.common.utils.PageReq;
import com.co.server.entity.vote.TLabel;
import com.co.server.entity.vote.aide.TLabelQuery;
import com.co.server.service.vote.TLabelService;
import com.co.vote.constant.HttpStatusCode;

@Controller
@RequestMapping("/label")
public class LabelController {

	@Resource
	private TLabelService tLabelService;

	private static final int pageSize = 100;
	
	
	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String list( Model model,HttpSession session,PageReq pageReq,TLabelQuery query ) {
		pageReq.setSort(new Sort(Direction.DESC,"t.create_time"));
		pageReq.setPageSize(pageSize);
		Page<TLabel> page = tLabelService.queryPageList(query, pageReq);
		model.addAttribute("page", page);
		return "/label/list";
	}
	
	@RequestMapping(value = "/add", method = { RequestMethod.GET,RequestMethod.POST })
	public String addInit( Model model,HttpSession session) {
		return "label/add";
	}

	@RequestMapping(value = "/edit/{id}/{pageNumber}", method = { RequestMethod.GET,RequestMethod.POST })
	public String edit( Model model,HttpSession session,@PathVariable Integer id, @PathVariable Integer pageNumber) {
		TLabel tLabel = tLabelService.queryById(id);
		model.addAttribute("tLabel", tLabel);
		model.addAttribute("pageNumber", pageNumber);
		return "label/edit";
	}

	@RequestMapping(value = "/save", method = { RequestMethod.GET,RequestMethod.POST })
	public String save(Model model,HttpSession session,TLabel tLabel,PageReq pageReq,RedirectAttributes redirectAttributes) {
		if(tLabel.getId()==null){
			tLabelService.add(tLabel);
		}else{
			tLabelService.updateByIdSelective(tLabel);
		}
		redirectAttributes.addAttribute("pageNumber", pageReq.getPageNumber());
		return "redirect:/label/list";
	}

	@ResponseBody
	@RequestMapping(value = "/delete/{id}", method = { RequestMethod.GET,RequestMethod.POST })
	public Map<String, Object> delete(@PathVariable Integer id) {
		tLabelService.deleteById(id);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", HttpStatusCode.CODE_SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/ajaxList", method = { RequestMethod.GET, RequestMethod.POST })
	public Map<String, Object> ajaxList( Model model,HttpSession session,PageReq pageReq,TLabelQuery query ) {
		pageReq.setSort(new Sort(Direction.DESC,"t.create_time"));
		pageReq.setPageSize(pageSize);
		Page<TLabel> page = tLabelService.queryPageList(query, pageReq);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", HttpStatusCode.CODE_SUCCESS);
		result.put("page", page);
		return result;
	}
}
